package com.nsgf.library.ui.page;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.MessageBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.response.DataResult;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.databinding.ActivityNsgfMessageDetailsBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.event.EventMsg;
import com.nsgf.library.event.RxBus;

import org.greenrobot.eventbus.EventBus;

public class MessageDetailsActivity extends BaseActivity {

    MessageStates states;

    private AccountRequester mAccountRequester;
    private boolean warning;

    @Override
    protected void initViewModel() {

        states = getActivityScopeViewModel(MessageStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_nsgf_message_details, BR.vm, states);
    }

    private ActivityNsgfMessageDetailsBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = (ActivityNsgfMessageDetailsBinding) getBinding();

        states.titleBar.set(getIntent().getStringExtra("title"));
        
        warning = getIntent().getBooleanExtra("warning", false);
        getLifecycle().addObserver(mAccountRequester);
        mAccountRequester.getMessageDetailResult().observe(this, new Observer<DataResult<ResponseStatus<MessageBean.DataBean>>>() {
            @Override
            public void onChanged(DataResult<ResponseStatus<MessageBean.DataBean>> messageBeanDataResult) {
                if (messageBeanDataResult.getResponseStatus().isSuccess()) {
                    if (messageBeanDataResult.getResponseStatus().getResponseCode() == 200) {

                        mBinding.setVmInfo(messageBeanDataResult.getResult().getData());
                    } else {

                        if (401 == messageBeanDataResult.getResult().getResponseCode()) {
                            UserInfo.getInstance().clearUserInfo();
                            finish();
                        }
                        showToast(messageBeanDataResult.getResult().getMsg());
                    }
                }
            }
        });
        mAccountRequester.getMessageDetail(getIntent().getStringExtra("id"), warning);

        EventMsg eventMsg = new EventMsg();
        eventMsg.setMsg("warning");
        RxBus.get().post(eventMsg);
        EventBus.getDefault().post(eventMsg);
    }

    public static class MessageStates extends StateHolder {

        public final State<String> name = new State<>("");

        public final State<Boolean> loadingVisible = new State<>(false);

    }

}
